package com.github.chain.admin.api.controller;



import com.github.chain.admin.client.MenuServiceClient;
import com.github.chain.admin.client.RoleServiceClient;
import com.github.chain.admin.common.domain.Role;
import com.github.chain.admin.common.domain.RoleMenu;
import com.github.chain.admin.common.domain.User;
import com.github.chain.admin.common.group.InserterGroup;
import com.github.chain.admin.common.group.UpdateGroup;
import com.github.chain.admin.common.request.RoleRequest;
import com.github.chain.admin.common.request.UserRequest;
import com.github.framework.core.page.DataPage;

import com.github.framework.core.Result;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author AIQ
 * @date 2023/5/12
 * @project er-trader
 */
@Controller
public class RoleController {

    @Resource
    private RoleServiceClient roleServiceClient;

    @Resource
    private MenuServiceClient menuServiceClient;


    @RequiresPermissions("system:role:index")
    @GetMapping("/main/system/role/index")
    public String index(RoleRequest roleRequest, Model model) {
        Result<DataPage<Role>> result = roleServiceClient.findRoleByPage(roleRequest);
        if (result.isSuccess()){
            DataPage<Role> dataPage = result.getData();
            List<Role> roleList =dataPage.getDataList();
            model.addAttribute("page",dataPage);
            model.addAttribute("list",roleList);
        }
        return "/manager/role/index";
    }

    @GetMapping("/system/role/edit/{id}")
    @RequiresPermissions("system:role:edit")
    public String roleEdit(@PathVariable("id") Long id, Model model) {

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Result<Role> result = roleServiceClient.findRoleById(id);

        if (result.isSuccess()) {
            model.addAttribute("role", result.getData());
            model.addAttribute("user", user);
        }
        return "/manager/role/edit";
    }

    @PostMapping("/system/role/edit")
    @RequiresPermissions("system:role:edit")
    @ResponseBody
    public Result<Integer> update(@Validated(value = UpdateGroup.class) RoleRequest roleRequest){

        return roleServiceClient.updateRole(roleRequest);
    }

    @GetMapping("/system/role/detail/{id}")
    @RequiresPermissions("system:role:detail")
    public String detail(@PathVariable("id") Long id, Model model){

        Result<Role> result = roleServiceClient.findRoleById(id);
        if (result.isSuccess()) {
            model.addAttribute("role", result.getData());
        }
        return "/manager/role/detail";
    }

    @GetMapping("/system/role/userList/{id}")
    @RequiresPermissions("system:role:detail")
    public String userList(@PathVariable("id") Long id, Model model){
        Result<List<User>> userByRoleId = roleServiceClient.findUserByRoleId(id);
        model.addAttribute("list", userByRoleId.getData());
        return "/manager/role/userList";
    }

    @GetMapping("/system/role/delete/{id}")
    @RequiresPermissions("system:role:delete")
    @ResponseBody
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return roleServiceClient.deleteRoleById(id);
    }

    @PostMapping("/system/role/status/delete")
    @RequiresPermissions("system:role:status:delete")
    @ResponseBody
    public Result deleteList(@RequestParam("ids") List<Long> list) {

        return roleServiceClient.deleteRoleListById(list);
    }

    @GetMapping("/system/role/add")
    @RequiresPermissions("system:role:add")
    public String add() {
        return "/manager/role/add";
    }

    @PostMapping("/system/role/save")
    @RequiresPermissions("system:role:save")
    @ResponseBody
    public Result save(@Validated(value = InserterGroup.class) RoleRequest roleRequest) {

        return roleServiceClient.saveRole(roleRequest);
    }

    @GetMapping("/system/role/auth")
    @RequiresPermissions("system:role:auth")
    public String roleAuth(@RequestParam("ids") Long id, Model model){

        model.addAttribute("id", id);
        return "/manager/role/auth";
    }

    @GetMapping("/system/role/authList/{id}")
    @RequiresPermissions("system:role:auth")
    @ResponseBody
    public Result roleMenuAuthList(@PathVariable("id") Long id){

        return menuServiceClient.roleMenuAuthList(id);
    }

    @PostMapping("/system/role/auth")
    @RequiresPermissions("system:role:auth")
    @ResponseBody
    public Result authPermissions(RoleMenu roleMenu){
        return roleServiceClient.authPermissions(roleMenu);
    }

    @PostMapping("/system/role/status/ok")
    @RequiresPermissions("system:role:status")
    @ResponseBody
    public Result enableStatus(@RequestParam List<Long> ids){
        return roleServiceClient.enableRoleStatus(ids);
    }

    @PostMapping("/system/role/status/freezed")
    @RequiresPermissions("system:role:status")
    @ResponseBody
    public Result disEnableStatus(@RequestParam List<Long> ids){
        return roleServiceClient.disableRoleStatus(ids);
    }

}
